package com.openness.spider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import com.openness.spider.commons.HBaseWritable;
import com.openness.spider.commons.MD5;
import com.openness.spider.commons.Statics;

/**
 * @author Rain
 * @version 2013-6-20 上午10:05:03
 */

public class Html implements HBaseWritable {

	private String rowkey;

	private int interest;

	private int category;

	private int website;

	private String url;

	private String html;

	private String htmlTitle;

	private String htmlKeywords;

	private String htmlDescription;

	private String textTitle;

	private String textContent;

	private String textSource;

	private String textTime;

	public Html() {
		this.rowkey = "";

		this.url = "";

		this.html = "";

		this.htmlTitle = "";

		this.htmlKeywords = "";

		this.htmlDescription = "";

		this.textTitle = "";

		this.textContent = "";

		this.textSource = "";

		this.textTime = "";
	}

	public Html(String url) {
		this();

		this.url = url;
	}

	public Html(String url, String html, String htmlTitle, String htmlKeywords,
			String htmlDescription, String textTitle, String textContent,
			String textSource, String textTime) {
		this.rowkey = "";

		this.interest = -1;

		this.category = -1;

		this.website = -1;

		this.url = url != null ? url : "";

		this.html = html != null ? html : "";

		this.htmlTitle = htmlTitle != null ? htmlTitle : "";

		this.htmlKeywords = htmlKeywords != null ? htmlKeywords : "";

		this.htmlDescription = htmlDescription != null ? htmlDescription : "";

		this.textTitle = textTitle != null ? textTitle : "";

		this.textContent = textContent != null ? textContent : "";

		this.textSource = textSource != null ? textSource : "";

		this.textTime = textTime != null ? textTime : "";
	}

	public String getRowkey() {
		return rowkey;
	}

	public int getInterest() {
		return interest;
	}

	public void setInterest(int interest) {
		this.interest = interest;
	}

	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public int getWebsite() {
		return website;
	}

	public void setWebsite(int website) {
		this.website = website;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getHtmlTitle() {
		return htmlTitle;
	}

	public void setHtmlTitle(String htmlTitle) {
		this.htmlTitle = htmlTitle;
	}

	public String getHtmlKeywords() {
		return htmlKeywords;
	}

	public void setHtmlKeywords(String htmlKeywords) {
		this.htmlKeywords = htmlKeywords;
	}

	public String getHtmlDescription() {
		return htmlDescription;
	}

	public void setHtmlDescription(String htmlDescription) {
		this.htmlDescription = htmlDescription;
	}

	public String getTextTitle() {
		return textTitle;
	}

	public void setTextTitle(String textTitle) {
		this.textTitle = textTitle;
	}

	public String getTextContent() {
		return textContent;
	}

	public void setTextContent(String textContent) {
		this.textContent = textContent;
	}

	public String getTextSource() {
		return textSource;
	}

	public void setTextSource(String textSource) {
		this.textSource = textSource;
	}

	public String getTextTime() {
		return textTime;
	}

	public void setTextTime(String textTime) {
		this.textTime = textTime;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof Html)) {
			return false;
		}

		Html page = (Html) obj;

		if (this.url.equals(page.url)) {
			return true;
		}

		return false;
	}

	@Override
	public int hashCode() {
		return this.url.hashCode();
	}

	@Override
	public String toString() {
		List<String> strings = new ArrayList<String>();

		strings.add("rowkey: " + rowkey);
		strings.add("interest: " + interest);
		strings.add("category: " + category);
		strings.add("website: " + website);
		strings.add("url: " + url);
		// strings.add("html: " + html);
		strings.add("htmlTitle: " + htmlTitle);
		strings.add("htmlKeywords: " + htmlKeywords);
		strings.add("htmlDescription: " + htmlDescription);
		strings.add("textTitle: " + textTitle);
		strings.add("textContent: " + textContent);
		strings.add("textSource: " + textSource);
		strings.add("textTime: " + textTime);

		return Arrays.toString(strings.toArray(new String[strings.size()]));
	}

	@Override
	public void get(Result result) {
		if (result == null || result.size() == 0) {
			return;
		}

		this.rowkey = Bytes.toString(result.getRow());

		for (KeyValue kv : result.raw()) {
			if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase("interest")) {
				this.interest = Bytes.toInt(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"category")) {
				this.category = Bytes.toInt(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"website")) {
				this.website = Bytes.toInt(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier())
					.equalsIgnoreCase("url")) {
				this.url = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"html")) {
				this.html = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"htmlTitle")) {
				this.htmlTitle = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"htmlKeywords")) {
				this.htmlKeywords = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"htmlDescription")) {
				this.htmlDescription = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"textTitle")) {
				this.textTitle = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"textContent")) {
				this.textContent = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"textSource")) {
				this.textSource = Bytes.toString(kv.getValue());
			} else if (Bytes.toString(kv.getQualifier()).equalsIgnoreCase(
					"textTime")) {
				this.textTime = Bytes.toString(kv.getValue());
			}
		}
	}

	@Override
	public Get get() {
		throw new RuntimeException("unsupported method get");
	}

	@Override
	public Put put() {
		Put put = new Put(Bytes.toBytes(System.currentTimeMillis()
				+ Statics.ROWKEY_SEPERATOR + MD5.md5(this.url)));

		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("interest"),
				Bytes.toBytes(this.interest));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("category"),
				Bytes.toBytes(this.category));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("website"),
				Bytes.toBytes(this.website));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("url"),
				Bytes.toBytes(this.url));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("html"),
				Bytes.toBytes(this.html));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("htmlTitle"),
				Bytes.toBytes(this.htmlTitle));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("htmlKeywords"),
				Bytes.toBytes(this.htmlKeywords));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("htmlDescription"),
				Bytes.toBytes(this.htmlDescription));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("textTitle"),
				Bytes.toBytes(this.textTitle));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("textContent"),
				Bytes.toBytes(this.textContent));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("textSource"),
				Bytes.toBytes(this.textSource));
		put.add(Statics.HBASE_FAMILY, Bytes.toBytes("textTime"),
				Bytes.toBytes(this.textTime));

		return put;
	}

	@Override
	public Delete delete() {
		throw new RuntimeException("unsupported method delete");
	}

}
